package org.xiaov.extra.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.xiaov.core.bean.BeanHelper;
import org.xiaov.execption.XiaovAuthException;
import org.xiaov.http.HttpCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @description: 关于session的认证
 * @author: xiaovcloud
 * @date: 2021/2/1 20:25
 * @version：1.0
 */
@Component
public class XiaovSessionInterceptor implements HandlerInterceptor {
    private static final Logger log = LoggerFactory.getLogger(XiaovSessionInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HttpSession session = request.getSession();
        Enumeration<String> attrs = session.getAttributeNames();
        if (BeanHelper.isEmpty(attrs)) {
            throw new XiaovAuthException(HttpCode.HTTP_FORBIDDEN.getValue(), "登录失效，请重新登录");
        }
        log.info("验证session成功!");
        return true;
    }
}
